package com.jgp.workflow.controller;

import com.jgp.security.config.SecUserAdapter;
import com.jgp.security.pojo.UserInfo;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.workflow.pojo.ViewProcessIns;
import com.jgp.workflow.service.ProcessService;
import com.jgp.workflow.utils.ProcessUtil;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Objects;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/11/14
 */
@CrossOrigin
@Controller
@RequestMapping("/workflow/processController")
public class ProcessController extends JGPBackController {
    
    @Autowired
    private SecUserAdapter userAdapter;
    
    @Autowired
    private HistoryService historyService;
    
    @Autowired
    private ProcessService processService;
    
    @RequestMapping("/listProcessDef")
    public String listProcessDef(Model model){
        UserInfo user = userAdapter.currentUser();
        re(model, "userId", Objects.nonNull(user)?user.getId():null);
        return page("listProcessDef");
    }
    
    @RequestMapping("/showProcessPicBeforeStart/{defKey}")
    public String showProcessPicBeforeStart(@PathVariable("defKey") String defKey, Model model){
        re(model,"defKey",defKey);
        return page("showProcessPicBeforeStart");
    }
    
    @RequestMapping("/showProcessPic/{processInsId}")
    public String showProcessPic(@PathVariable("processInsId") String processInsId, Model model){
        HistoricProcessInstance instance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInsId).singleResult();
        re(model,"procDefId",instance.getProcessDefinitionId());
        re(model,"procId",processInsId);
        re(model,"procName",instance.getProcessDefinitionName());
        return page("designer/diagram-viewer/index");
    }
    
    @RequestMapping("/showNotPassTaskMgr")
    public String showNotPassTaskMgr(Model model){
        UserInfo user = userAdapter.currentUser();
        re(model, "userId", Objects.nonNull(user)?user.getId():null);
        return page("showNotPassTaskMgr");
    }
    
    /**
     * 查询待办任务
     * @param model
     * @return
     */
    @RequestMapping("/listMyToDoTask")
    public String listMyToDoTask(Model model){
        UserInfo user = userAdapter.currentUser();
        re(model, "userId", Objects.nonNull(user)?user.getId():null);
        return page("listMyToDoTask");
    }
    /**
     * 查询已办任务
     * @param model
     * @return
     */
    @RequestMapping("/listDidTask")
    public String listDidTask(Model model){
        UserInfo user = userAdapter.currentUser();
        re(model, "userId", Objects.nonNull(user)?user.getId():null);
        return page("listDidTask");
    }
    
    /**
     * 查询我发起的任务
     * @param model
     * @return
     */
    @RequestMapping("/listMyWorkOrder")
    public String listMyInitiatedTask(Model model){
        UserInfo user = userAdapter.currentUser();
        re(model, "userId", Objects.nonNull(user)?user.getId():null);
        return page("listMyWorkOrder");
    }
    
    @RequestMapping("/showDefaultTaskForm")
    public String showDefaultTaskForm(String procId, String taskId,Boolean editFlag,String claimState,String businessKey,Model model) {
        UserInfo user = userAdapter.currentUser();
        re(model,"procId",procId);
        re(model,"taskId",taskId);
        re(model,"userId",Objects.nonNull(user)?user.getId():null);
        re(model,"editFlag",editFlag);
        re(model,"claimState",claimState);
        re(model,"businessKey",businessKey);
        re(model,"comments",processService.queryTaskComments(taskId));
        return page("showDefaultTaskForm");
    }
    
    @RequestMapping("/showTaskForm")
    public String showTaskForm(String procId, String taskId,Boolean editFlag,String claimState,String businessKey,Model model) {
        UserInfo user = userAdapter.currentUser();
        re(model,"procId",procId);
        re(model,"taskId",taskId);
        re(model,"claimState",StringUtils.isBlank(claimState)?"":claimState);
        re(model,"editFlag",editFlag);
        re(model,"businessKey",businessKey);
        String formKey = ProcessUtil.getCustomForm(procId, taskId,user.getId(),editFlag);
        if(StringUtils.isNotBlank(formKey)){
            if(formKey.charAt(0) != '/'){
                formKey = "/"+formKey;
            }
        }
        re(model,"busiForm",formKey);
        return page("showTaskForm");
    }
    
    @RequestMapping("/showProcFlowed")
    public String showProcFlowed(String procId,String sn,String businessKey,Model model) {
        if(StringUtils.isBlank(procId)){
            if(StringUtils.isNotBlank(sn)){
                ViewProcessIns proc = processService.queryOneProcessInsBySn(sn);
                procId = proc.getId();
            }else if(StringUtils.isNotBlank(businessKey)){
                ViewProcessIns proc = processService.queryOneProcessInsByBusinessKey(businessKey);
                procId = proc.getId();
            }
        }
        re(model,"procId",procId);
        return page("showProcFlowed");
    }
    
    @RequestMapping("/showTaskComments/{taskId}")
    public String showTaskComments(@PathVariable("taskId") String taskId,Model model) {
        re(model,"taskId",taskId);
        return page("showTaskComments");
    }
    
    
    @RequestMapping("/showTaskFormWithOutTaskFlow")
    public String showTaskFormWithOutTaskFlow(String procId, String taskId,Boolean editFlag,Model model) {
        UserInfo user = userAdapter.currentUser();
        re(model,"procId",procId);
        re(model,"taskId",taskId);
        re(model,"editFlag",editFlag);
        String formKey = ProcessUtil.getCustomForm(procId, taskId,user.getId(),editFlag);
        if(StringUtils.isNotBlank(formKey)){
            if(formKey.charAt(0) != '/'){
                formKey = "/"+formKey;
            }
        }
        re(model,"busiForm",formKey);
        return page("showTaskFormWithOutTaskFlow");
    }
    
    @RequestMapping("/listTaskFlowed")
    public String listTaskFlowed(String procId,Model model) {
        re(model,"procId",procId);
        return page("listTaskFlowed");
    }
    
    
    @RequestMapping("test1")
    public String test1(String procId, String taskId,String businessKey,Model model) {
        re(model,"procId",procId);
        re(model,"taskId",taskId);
        re(model,"businessKey",businessKey);
        return page("test1");
    }
    
}
